//
//  CNShoppingViewController.m
//  BeeQuick_19
//
//  Created by w on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNShoppingViewController.h"
#import "CNShopCarMarkerView.h"
#import "CNShopCarCell.h"
#import "CNUserShopCarTools.h"
#import "CNShopCarCommentsView.h"
#import "CNShopCarSignTimeView.h"
#import "CNReceiveAdressView.h"
#import "CNEmptyShopCarView.h"
#import "CNdetailsCell.h"
#import "CNConsigneeAddressTableViewController.h"
#import "CNComitOrderTableViewController.h"
#import "UIView+CNViewController.h"
@interface CNShoppingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong,nonatomic) CNdetailsModel * model;
//headerView
@property(nonatomic,strong) UIView *tableHeaderView;
//tableView
@property(nonatomic,strong) UITableView *tableView;
//空购物车
@property(nonatomic,strong) CNEmptyShopCarView *emptyView;
//地址
@property(nonatomic,strong) CNReceiveAdressView *CNReceiveAdressView;
//购物数量
@property (nonatomic,assign) NSInteger userBuyNumber;

//设置取消第一响应者控制器属性
@property (nonatomic, strong) CNShopCarCommentsView *carView;

@end

@implementation CNShoppingViewController


- (void)viewDidLoad {
    //这里初始化视图
//    self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
    self.title = @"购物车";
    //添加通知
    [self addNotification];
    [self buildTableView];
    [super viewDidLoad];
    [self buildCNEmptyShopCarView];
    //    UITabBar *item = [UITabBar appearance];
    //    item.tintColor = [UIColor orangeColor];
    //    UINavigationBar *navgationBar = [UINavigationBar appearance];
    //    navgationBar.translucent = NO;
    
    //添加navItem的左侧按钮
    UIBarButtonItem *itemB = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStyleDone target:self action:@selector(backVC)];
    self.navigationItem.leftBarButtonItem = itemB;
    itemB.tintColor = [UIColor darkGrayColor];
    //设置颜色
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:252/255.0 green:210/255.0 blue:48/255.0 alpha:1.0]];
}
//视图将要显示的时候进行判断
//如果购物车数据为空显示空购物车视图数据 隐藏tableView 否则提示用户信息正在加载中
- (void)viewWillAppear:(BOOL)animated {
    
    self.longFooterView.partner_price = 1.0;
    // if ([[CNUserShopCarTools sharedInstance]isEmpty]) {
    //    self.emptyView.hidden = NO;
    // self.tableView.hidden = YES;
    // }else {
       self.emptyView.hidden = YES;
        //开启用户交互(备用代码)
       self.emptyView.userInteractionEnabled = YES;
       self.view.userInteractionEnabled = YES;
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0]];
        [SVProgressHUD showWithStatus:@"信息正在加载中"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.shopCar = [[SQLiteManager sharedSQLiteManager] queryContact];
            [self.tableView reloadData];
            [SVProgressHUD dismiss];
            self.tableView.hidden = NO;

        });
   // }
}
#pragma mark 添加购物车通知
- (void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRemoveProduct) name:CNShopCarDidRemoveProductNSNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buyNumberChange) name:CNShopCarBuyNumberDidChangeNotification object:nil];
}

#pragma mark 创建tableview视图
- (void)buildTableView{
    //设置tableView
    self.tableView = ({
        UITableView *view =[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        view.delegate = self;
        view.dataSource = self;
        //不显示垂直方向的滚动条
        view.showsVerticalScrollIndicator = NO;
        view.hidden = YES;
        view.backgroundColor = [UIColor clearColor];
        //去掉cell的分割线
        view.separatorStyle = UITableViewCellSeparatorStyleNone;
        //enum
        view;
    });
    [self.view addSubview:self.tableView];
    //布局tableView
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    //方法中同时添加上面的headerView 和下面的footerView
    [self buildtableFooterView];
    [self buildtableHeaderView];

}
#pragma mark 创建Headerview视图
- (void)buildtableHeaderView{
   
//    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(300, 100, Width, 250)];
//    self.tableHeaderView.backgroundColor = self.view.backgroundColor;
//    self.tableHeaderView.userInteractionEnabled = YES;

    //自定义四个购物车分类视图
    CNReceiveAdressView *addressView = [[CNReceiveAdressView alloc]initWithFrame:CGRectMake(0, 10, Width, 70)];
    addressView.navController = self.navigationController;
    //将默认的地址添加到地址显示视图进行显示
    //addressView.address = [UserInfo sharedInstance].defaultAddress;
    CNShopCarMarkerView *marketView = [[CNShopCarMarkerView alloc]initWithFrame:CGRectMake(0, 90, Width, 60)];
    CNShopCarSignTimeView *signTimeView = [[CNShopCarSignTimeView alloc]initWithFrame:CGRectMake(0, 150, Width, 50)];
    
    CNShopCarCommentsView *commentsView = [[CNShopCarCommentsView alloc]initWithFrame:CGRectMake(0, 200, Width, 50)];
    //设置取消第一响应者控制器
    self.carView = commentsView;

#pragma mark 弱引用控制器同时push到修改地址控制器
    __weak typeof(self) weakSelf = self;
    self.CNReceiveAdressView.modifyBtnCliked = ^{
        [weakSelf.navigationController pushViewController:[CNConsigneeAddressTableViewController new] animated:YES];
    };
    
    //将视图添加到自定义的边框视图
    [self addToHeadViewWithBorder:addressView];
    [self addToHeadViewWithBorder:marketView];
    [self addToHeadViewWithBorder:signTimeView];
    [self addToHeadViewWithBorder:commentsView];
    self.tableView.tableHeaderView = self.tableHeaderView;
    self.tableHeaderView.userInteractionEnabled = YES;
    ;
}
#pragma mark 创建footerView视图
- (void)buildtableFooterView{
    self.longFooterView = [[CNShopCarTableFooterView alloc]initWithFrame:CGRectMake(0, 0, Width, 50)];
    //将视图添加到自定义的边框视图
    [self addToHeadViewWithBorder:_longFooterView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    
#pragma mark 让底部的价格视图进行显示
    self.longFooterView.partner_price = [[CNUserShopCarTools sharedInstance] getShopCarGoodsPrice];
    
#pragma mark 弱引用控制器同时push到付款界面
    __weak typeof(self) weakSelf = self;
    self.longFooterView.sureButtonClicked = ^{
    [weakSelf.navigationController pushViewController:[CNComitOrderTableViewController new] animated:YES];
    };
    self.tableView.tableFooterView = self.longFooterView;
}
#pragma mark 创建空购物车显示视图
- (void)buildCNEmptyShopCarView{
    self.emptyView = [[CNEmptyShopCarView alloc]init];
    self.emptyView.hidden = YES;
    [self.view addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(170);
    }];
}
//移除购物车数据让tableView直接隐藏 空购物车显示
- (void)didRemoveProduct{
    if(self.model.count == 0){
        self.emptyView.hidden = NO;
        self.tableView.hidden = YES;
        return;
    }
//刷新tableView数据
    [self.tableView reloadData];
}

//底部价格改变的显示
- (void)buyNumberChange{
    
    self.longFooterView.partner_price = [[CNUserShopCarTools sharedInstance]getShopCarGoodsPrice];
}

#pragma mark 将自定义的view添加到table上
- (void)addToHeadViewWithBorder:(UIView *)view{
    view.backgroundColor = [UIColor whiteColor];
    //设置view的边框的宽度
    view.layer.borderWidth = 0.5;
    view.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0].CGColor;
    [self.tableView addSubview:view];
}

#pragma mark dataSource /delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //返回购物车的数量cell
    return self.shopCar.count;
   //return ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //将自定义的购物车cell进行传值给 购物商品并返回cell
    CNShopCarCell *cell = [CNShopCarCell cellWithTableView:tableView];
    cell.goods = self.shopCar[indexPath.row];
    cell.tableView = self.tableView;
    return cell;
}
//设置tableView的行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 215;
}
//跳转到首页控制器 使用递归方式找到控制器
-(void)backVC
{
       self.navigationController.tabBarController.selectedIndex = 0;
}
//取消第一响应者
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    for (UIView *v in self.carView.subviews) {
        [v resignFirstResponder];
    }
}


@end
